"""
A class to exercise the JT core functions.
"""
import JT
import Calibration
import unittest
from IPython.Shell import IPShellEmbed
import cPickle
import GDMetric2
import nMIMetric

class Test(unittest.TestCase):
    def runTest(self):
        """
        Test out the ITK get metric procedure.
        """

#===============================================================================
#    Data Paths
#===============================================================================

        fixedImageName = 'C:/Users/bryan/bryan-code/2D3D/vert1/fluoro/ushortim080-LAT.mhd'
#        fixedImageName = 'C:/Users/bryan/bryan-code/trunk/Images/CalibratedDRRImage.mhd'
        fixedImageMaskName = 'C:/Users/bryan/bryan-code/2D3D/vert1/fluoro/ucharb2Rmask080-LAT.mha'
        movingImageName = 'C:/Users/bryan/bryan-code/trunk/Images/CalibratedDRRImage.mhd'
        inputVolFileName = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1.mha'
        staFile = 'C:/Users/bryan/bryan-code/2D3D/vert1/CT/ucharCTv1r1.sta'
        calFileLat = 'C:/Users/bryan/bryan-code/trunk/TestData/ext_cal1.txt'
        outputDirectory = 'C:/Users/bryan/bryan-code/trunk/Images/debug/'
        
        #For Testing
        T = JT.Testing()

#===============================================================================
#    Setup DRR
#===============================================================================
        print "Setting up DRR..."
        xraycam = JT.XRayCamera()
        drr = JT.DRR()
        drr.SetXRayCamera(xraycam)
        drr.SetVolumeFilename(inputVolFileName)
        drr.SetBlackOnWhiteImage()
        drr.InteractionOff()

        cal = Calibration.ExternalCalibration()
        cal.LoadConsolidatedCalibration(calFileLat)
        cal.LoadStaFile(staFile)

        xraycam.SetExternalCalibration(cal)

        drr._volume.SetOriginalTransform(cal._VolumeT)
        drr._volume.SetBodyCenter((3.906, 2.17, -7.378))
        drr.SetBlackOnWhiteImage()

#===============================================================================
#    Real-life 3D-2D metric (using image generator (DRR))
#===============================================================================
        print "Setting up Registraion..."
        reg=JT.NewRegistration()
        reg.SetFixedFileName(fixedImageName)
        reg.SetFixedImageMask(fixedImageMaskName)
        reg.SetMovingImageGenerator(drr)
        roi = [[210,187],[277,370]]
        reg.SetRegionOfInterest(roi)
        
        drr.Render()
        
#===============================================================================
#    Setup Optimizer
#===============================================================================
        print "Setting up Optimizer..."
        opt = JT.Optimizer()
        opt.SetFunction(reg.GetValue)   
        opt.SetRegistration(reg)   
        
        # Set Parameters #
        nPose = 50
        method = 'ncc'
        reg.SetImageMetric(method)
        pose = T.GetStartPose(nPose)
        print "Starting Pose: ", pose
        opt.SetInitialParameters(pose) 
        opt._minimize = True
        opt._stepSize = 0.2
        opt._nLevels = 4
        opt._plot = True
        #opt.SaveImageAtEachIteration(outputDirectory)         
        print "Starting Optimizer..."
        params,val = opt.Optimize()
        print "Solution Found   :", params,val
        print "Starting Position: ", pose

#===============================================================================
#    Print Results
#===============================================================================
        print "Done"
        return params, val
        

if __name__ == "__main__":
    test=Test()
    params, val = test.runTest()
